﻿<?php require_once('Connections/satphp.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}


if(@$_GET['iddisciplina']=="" && @$_GET['idaula']=="")
		header ("Location: aula-listar.php?iddisciplina=0");

if(@isset($_GET['iddisciplina'])){
	$iddisciplina = @$_GET['iddisciplina'];
	mysql_select_db($database_satphp, $satphp);
	$query_Recordset3 = "SELECT disciplina.nome FROM disciplina WHERE iddisciplina = $iddisciplina";
	$Recordset3 = mysql_query($query_Recordset3, $satphp) or die(mysql_error());
	$row_Recordset3 = mysql_fetch_assoc($Recordset3);
	$totalRows_Recordset3 = mysql_num_rows($Recordset3);
}

if(@isset($_GET['idaula']) || @isset($_GET['edita'])){

	$idaula = @$_GET['idaula'];
	mysql_select_db($database_satphp, $satphp);
	$query_Recordset1 = "SELECT * FROM aula JOIN disciplina ON (aula.iddisciplina = disciplina.iddisciplina) WHERE aula.idaula = $idaula";
	$Recordset1 = mysql_query($query_Recordset1, $satphp) or die(mysql_error());
	$row_Recordset1 = mysql_fetch_assoc($Recordset1);
	
	if (@isset($_GET['edita'])){
		$data = $_POST['data'];
		$conteudo = $_POST['conteudo'];
		$aulas = $_POST['aulas'];
		
		mysql_select_db($database_satphp, $satphp);
		$query_Recordset2 = "UPDATE aula SET data = '$data', conteudo = '$conteudo', aulas = $aulas WHERE idaula = $idaula";
		$Recordset2 = mysql_query($query_Recordset2, $satphp) or die(mysql_error());
		@$row_Recordset2 = mysql_fetch_assoc($Recordset2);
		$iddisciplina = $row_Recordset1['iddisciplina'];
		header ("Location: aula-listar.php?iddisciplina=$iddisciplina");
	}
}else{
	$editFormAction = $_SERVER['PHP_SELF'];
	if (isset($_SERVER['QUERY_STRING'])) {
	  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
	}
	
	if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
	  $insertSQL = sprintf("INSERT INTO aula (iddisciplina, `data`, conteudo, aulas) VALUES (%s, %s, %s, %s)",
					   
						   GetSQLValueString($_POST['iddisciplina'], "int"),
						   GetSQLValueString($_POST['data'], "date"),
						   GetSQLValueString($_POST['conteudo'], "text"),
						   GetSQLValueString($_POST['aulas'], "int"));
	
	  mysql_select_db($database_satphp, $satphp);
	  $Result1 = mysql_query($insertSQL, $satphp) or die(mysql_error());
	  $iddisciplina = @$_GET['iddisciplina'];
	  header("Location: aula-listar.php?iddisciplina=$iddisciplina");
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> <?php if (@$_GET['idaula']) echo "Editar | SATPHP"; else echo "Nova Aula | SATPHP";?></title>
<style type="text/css">
textarea{
	width:210px;
	height:40px;
	max-height:60px;
	resize:vertical;
}
</style>
</head>

<body>
<p>
<h1 align="center"><?php if (@isset($_GET['idaula'])) echo "Editar Aula"; else echo "Nova Aula de ".$row_Recordset3['nome'];?></h1>
</p>
<form action="<?php if (@$_GET['idaula']) echo "aula-nova.php?idaula=$idaula&edita=true"; else echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table align="center">
  <tr valign="baseline">
      <td colspan="2"><input name="iddisciplina" type="hidden" value="<?php if (@$_GET['idaula'])echo $_GET['idaula']; ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td colspan="2"><input name="iddisciplina" type="hidden" value="<?php if (@$_GET['iddisciplina'])echo $_GET['iddisciplina'];else echo @$row_Recordset1['iddisciplina']; ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Data:</td>
      <td><input type="text" name="data" value="<?php if (@$_GET['idaula'])echo $row_Recordset1['data']; ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td align="right" valign="top" nowrap="nowrap">Conteúdo:</td>
      <td><textarea name="conteudo" maxlength="45"><?php if (@$_GET['idaula'])echo $row_Recordset1['conteudo']; ?></textarea></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Qtd. Aulas:</td>
      <td><input type="text" name="aulas" value="<?php if (@$_GET['idaula'])echo $row_Recordset1['aulas']; ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Salvar" /></td>
    </tr>
  </table>
  <input type="hidden" name="idaula" value="" />
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
</body>
</html>